
package mezzo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.sql.PreparedStatement;

import mon.jr;

@SuppressWarnings({"unused", "rawtypes", "unchecked"})
public class DefaultMezzo{
	
	private int id = 0;
	protected HashMap data = null;
	
	private String __tabla(){
		
		if( this instanceof CD ) 	return "amadeus.discos";
		if( this instanceof Decca ) return "amadeus.discograficas";
		
		return null;
	}
	
	// Obtener el campo de búsqueda en función de la clase del objeto
	// A esta función llama "find"
	private String __campo_busqueda(){
		if( this instanceof CD )	return "numero_de_serie";
		if( this instanceof Decca ) return "etiqueta";
		
		return null;
	}
	
	protected boolean __isNumber(String value){
		
		value = value.trim();
		if (value.isEmpty() ) return false;
		for( int x = 0; x < value.length(); x++ )
			if( !Character.isDigit( value.charAt(x) ) ) return false;
		
		return true;
	}	
	 
	protected DefaultMezzo(){}
	
	protected  DefaultMezzo(HashMap map){
		this.set(map);
	}
	
	// llaman: add - update
	// si es INSERT en la cadena de consulta deberá darse el parámetro "?" 
	// para el incremento numérico en el campo "id"
	private int __update(String query){
		int rows = 0; 
		int x = 0;		
		
		jr j = jr.get();
		try{
			PreparedStatement ps = jr.cursor().prepareStatement(query);
			if( query.toLowerCase().startsWith("insert") ){
				x = j.inc(this.__tabla(), null);
				ps.setInt(1, x);
			}
			rows = ps.executeUpdate();
			if( x != 0 )  this.id = x;
		}
		catch(Exception e){}				
		j.close();
		
		return rows;
	}
	
	public boolean add(){
		
		StringBuffer query = new StringBuffer(String.format("INSERT INTO %s\n(",
				this.__tabla())); 
		Set <String> campos = this.data.keySet();
		
		query.append( campos.toString().substring(1, campos.toString().length() - 1)  );
		query.append(", id)\nVALUES(");
		for(String campo : campos){
			Object o = this.data.get(campo);
			if( o instanceof Integer )
				query.append( String.format("%s,\n", o) );
			else
				query.append( String.format("\"%s\",\n", o) );
		}
		query.append( "?)\n" );
		
		
		return (1 == this.__update(query.toString()));
	}  
	
	public boolean update(){
		StringBuffer query = new StringBuffer( String.format("UPDATE %s set\n", this.__tabla()) );
		Set <String> campos = this.data.keySet();
		for( String campo : campos ){
			Object obj = this.data.get(campo);
			if( obj instanceof Integer )
				query.append( String.format("%s = %s,\n", campo, obj.toString() ) );
			else
				query.append( String.format("%s = \"%s\",\n", campo, obj.toString() ) );
		}
		query.append("modificado = curdate()\n");
		query.append( String.format("where id = %d", this.id) );
		
		
		return ( 1 == this.__update(query.toString()) );
	}
	
	public boolean remove(){
		return false;
	}
	
	public int get(){ return this.id; }
	
	public void clear(){
		
		Set<String> campos = this.data.keySet();
		for (String campo : campos){
			Object obj = this.data.get(campo);
			if( obj instanceof Integer )
				this.data.put(campo, 0);
			else
				this.data.put(campo, "");
		}
		this.id = 0;
		
	}
	
	public void show(){
		
		Set<String> campos = this.data.keySet();
		for( String campo : campos)
			System.out.println( String.format("%s: %s", campo, this.data.get(campo))   );
		
		System.out.println( String.format("Núm.: %04d", this.id) );
		
	}	
	
	public void set(HashMap map){
		Set<String> campos = this.data.keySet();
		for(String campo : campos){
			Object obj = map.get(campo);
			this.data.put(campo, obj);
		}
		this.id = Integer.parseInt( map.get("id").toString() );
	}
	
	public void find(Object value){
		
		StringBuffer sb = new StringBuffer("SELECT DISTINCT ");
		Set<String> campos = this.data.keySet();
		for(String campo : campos) sb.append( campo + ", " );
		sb.append("id\n");
		sb.append( "from " + this.__tabla() + "\n" );
		// condición
		if( value instanceof Integer )
			sb.append( String.format("where id = %s\n", value.toString()) );
		else
			sb.append( String.format("where %s = \"%s\"", this.__campo_busqueda(), value.toString() ) );
		
		jr j = jr.get();
		ArrayList<HashMap> l = j.execute(sb.toString());
		j.close();
		
		
		if( l != null && l.size() == 1 ){
			HashMap map = l.get(0);
			this.id = Integer.parseInt(map.get("id").toString());
			for(String campo : campos){
				this.data.put(campo, map.get(campo));
			}
		}
		
		
	}
	
}

 